package com.onlyyou.stock.controller;

import com.alibaba.fastjson.JSONObject;
import com.onlyyou.stock.entity.Stock;
import com.onlyyou.stock.service.StockService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * @Author: 小野
 * @Date: 2020/1/11 14:04
 */
@RestController
@Api(tags = "商品相关接口")
@RequestMapping("/")
@CrossOrigin
public class StockController {
    @Autowired
    private StockService iStockService;

    @ApiOperation(value = "添加商品")
    @PostMapping("/save")
    public Map<String,Object> save(Long spuId,String name,Integer stockNum,Long price,String indexes,
                                   String ownSpec,Byte enable){
        Stock stock=new Stock();
        stock.setId(System.currentTimeMillis());
        stock.setSpuId(spuId);
        stock.setTitle(name);
        stock.setStock(stockNum);
        stock.setPrice(price);
        stock.setIndexes(indexes);
        stock.setOwnSpec(ownSpec);
        stock.setEnable(enable);
        stock.setCreateTime(new Date());
        return iStockService.add(stock.buildInfo());

    }

    @ApiOperation(value = "查询所有商品")
    @RequestMapping(value = "/getStockList")
    public Map<String, Object> getStockList() {
        return iStockService.getStockList();
    }

    @ApiOperation(value = "根据商品ID查询商品")
    @RequestMapping(value = "/getStock/{sku_id}")
    public Map<String, Object> getStock(@PathVariable("sku_id") String sku_id) {
        return iStockService.getStock(sku_id);
    }


    @ApiOperation(value = "新增秒杀政策")
    @RequestMapping(value = "/insertLimitPolicy")
    public Map<String, Object> insertLimitPolicy(String sku_id,String quanty,String begin_time,String end_time,String price) {
        Map<String, Object> policyInfo = new HashMap<>();
        policyInfo.put("sku_id",sku_id);
        policyInfo.put("quanty",quanty);
        policyInfo.put("begin_time",begin_time);
        policyInfo.put("end_time",end_time);
        policyInfo.put("price",price);
        return iStockService.insertLimitPolicy(policyInfo);
    }

    @GetMapping("/findAll")
    @ApiOperation(value = "分页查询")
    public Map<String,Object> findAll(Integer page, Integer limit) {
        return iStockService.findAll(page-1,limit);
    }
}
